package transforms;
import java.util.Iterator;

import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.FieldInsnNode;
import org.objectweb.asm.tree.MethodNode;

import com.rshacking.rhf.files.ClassContainer;
import com.rshacking.rhf.unification.InstructionItem;
import com.rshacking.rhf.updater.Transform;
import com.rshacking.rhf.updater.hooks.ClassHook;
import com.rshacking.rhf.updater.hooks.HooksList;
import com.rshacking.rhf.updater.instructions.InsnCondition;
import com.rshacking.rhf.updater.instructions.InstructionFinder;
import com.rshacking.rhf.updater.instructions.RegexInsnFinder;

public class CharacterTransforms extends Transform {

	@Override
	public boolean canActivate(HooksList hooks, ClassContainer cc) {
		return hooks.contains("Character") && cc.getName().equals(hooks.get("Character").getOrigName());
	}

	@Override
	public boolean execute(HooksList hooks, ClassContainer cc) {

		Iterator<MethodNode> methods = cc.getASM().methods.iterator();
		while (methods.hasNext()) {
			MethodNode method = methods.next();

			if (method.desc.equals("(I)V")) {
				InstructionFinder<AbstractInsnNode> finder = new InstructionFinder<AbstractInsnNode>(
						method, true);
				FieldInsnNode fin = null;

				fin = (FieldInsnNode) finder
						.getNext(InsnCondition.ASM.PUTFIELD_I);

				if (fin != null) {
					fin = (FieldInsnNode) finder
					.getNext(InsnCondition.ASM.PUTFIELD_I);
					if (fin != null) {
						hooks.get("Character").add("movingStatus", fin);
					}
				}

				return true;
			}
		}
		return false;
	}
}